package com.wonders.module.system.controller;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wonders.common.constant.TipConstant;
import com.wonders.framework.config.annotation.NotLogon;
import com.wonders.framework.util.ValidateUtil;
import com.wonders.framework.util.web.RequestUtil;
import com.wonders.framework.web.BaseController;
import com.wonders.module.auth.entity.vo.UserVo;
import com.wonders.module.auth.service.UserService;
import com.wonders.module.system.constant.SystemConstant;
import com.wonders.module.system.service.SystemService;

/**
 * 系统操作控制层
 * 
 * @author WuBin
 * 
 */
@Controller
@RequestMapping("/system")
public class SystemController extends BaseController {

	@Autowired
	private SystemService systemService;

	@Autowired
	private UserService userService;

	/**
	 * 导航到登录页面
	 * 
	 * @return
	 */
	@NotLogon
	@RequestMapping("toLogon")
	public Object toLogon() {
		return "system/logon";
	}

	/**
	 * 用户登录
	 * 
	 * @return
	 */
	@NotLogon
	@RequestMapping("logon")
	@ResponseBody
	public Object logon() {
		UserVo userVo = RequestUtil.requestParamToBean(UserVo.class);
		if (!ValidateUtil.nullCheck(userVo, "loginName", "password")) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		userVo.setPassword(DigestUtils.md5Hex(userVo.getPassword()));
		userVo = systemService.getUser(userVo);
		if (userVo != null) {
			RequestUtil.setSessionAttr(SystemConstant.LOGON_USER, userVo);
			return super.ajaxSuccess();
		}
		return super.ajaxError("用户名或密码错误");
	}

	/**
	 * 导航的主页面
	 * 
	 * @return
	 */
	@RequestMapping("toIndex")
	public String toIndex() {
		return "system/frame";
	}

	/**
	 * 获取用户菜单信息
	 * 
	 * @return
	 */
	@RequestMapping("getUserMenu")
	@ResponseBody
	public Object getUserMenu() {
		UserVo logonUser = RequestUtil.getSessionAttr(
				SystemConstant.LOGON_USER, UserVo.class);
		return systemService.getUserMenu(logonUser.getId());
	}

	/**
	 * 导航到欢迎页
	 * 
	 * @return
	 */
	@RequestMapping("toWelcome")
	public String toWelcome() {
		return "system/welcome";
	}

	/**
	 * 用户注销
	 * 
	 * @return
	 */
	@RequestMapping("logout")
	@ResponseBody
	public Object logout() {
		RequestUtil.getSession().invalidate();
		return super.ajaxSuccess();
	}

	/**
	 * 导航到修改密码页面
	 * 
	 * @return
	 */
	@RequestMapping("toEditPassword")
	public Object toEditPassword() {
		return "system/editPassword";
	}

	/**
	 * 修改密码
	 * 
	 * @return
	 */
	@RequestMapping("editPassword")
	@ResponseBody
	public Object editPassword() {
		UserVo userVo = RequestUtil.requestParamToBean(UserVo.class);
		if (!ValidateUtil.nullCheck(userVo, "originPassword", "password")) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		UserVo logonUser = RequestUtil.getSessionAttr(
				SystemConstant.LOGON_USER, UserVo.class);
		UserVo databaseUser = userService.load(logonUser.getId());
		if (!databaseUser.getPassword().equals(
				DigestUtils.md5Hex(userVo.getOriginPassword()))) {
			return super.ajaxError("旧密码输入不正确");
		}
		userVo.setPassword(DigestUtils.md5Hex(userVo.getPassword()));
		userVo.setId(logonUser.getId());
		systemService.editPassword(userVo);
		return super.ajaxSuccess();
	}

}
